package com.wh.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
 * 
 *  Class Name: BufferTest.java
 *  Description: java nio操作
 *  @author wanghao  DateTime 2017年1月17日 下午2:58:06 
 *  @company hongbo
 *  @email 1228675455@qq.com
 */
public class BufferTest {

	public static void main(String[] args) {
		RandomAccessFile randomAccessFile = null;
		try {
			randomAccessFile = new RandomAccessFile("C://Users//wh//Desktop//openid上传文件说明(1).txt", "rw");
			FileChannel channel = randomAccessFile.getChannel();
			ByteBuffer buf = ByteBuffer.allocate(48);
			for(int i = 0; (i = channel.read(buf)) != -1; ) {
				buf.flip();		//make buffer ready for read
				while(buf.hasRemaining()) {
					 System.out.print((char) buf.get()); // read 1 byte at a time
				}
				buf.clear();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				randomAccessFile.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	} 
}
